<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<!-- 
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License. 
-->
<html>
<head>
    <link type="text/css" rel="stylesheet" href="https://struts.apache.org/css/default.css">
    <style type="text/css">
        .dp-highlighter {
            width:95% !important;
        }
    </style>
    <style type="text/css">
        .footer {
            background-image:      url('https://cwiki.apache.org/confluence/images/border/border_bottom.gif');
            background-repeat:     repeat-x;
            background-position:   left top;
            padding-top:           4px;
            color:                 #666;
        }
    </style>
    <link href='https://struts.apache.org/highlighter/style/shCoreStruts.css' rel='stylesheet' type='text/css' />
    <link href='https://struts.apache.org/highlighter/style/shThemeStruts.css' rel='stylesheet' type='text/css' />
    <script src='https://struts.apache.org/highlighter/js/shCore.js' type='text/javascript'></script>
    <script src='https://struts.apache.org/highlighter/js/shBrushPlain.js' type='text/javascript'></script>
    <script src='https://struts.apache.org/highlighter/js/shBrushXml.js' type='text/javascript'></script>
    <script src='https://struts.apache.org/highlighter/js/shBrushJava.js' type='text/javascript'></script>
    <script src='https://struts.apache.org/highlighter/js/shBrushJScript.js' type='text/javascript'></script>
    <script src='https://struts.apache.org/highlighter/js/shBrushGroovy.js' type='text/javascript'></script>
    <script src='https://struts.apache.org/highlighter/js/shBrushBash.js' type='text/javascript'></script>
    <script type="text/javascript">
        SyntaxHighlighter.defaults['toolbar'] = false;
        SyntaxHighlighter.all();
    </script>
    <script type="text/javascript" language="javascript">
        var hide = null;
        var show = null;
        var children = null;

        function init() {
            /* Search form initialization */
            var form = document.forms['search'];
            if (form != null) {
                form.elements['domains'].value = location.hostname;
                form.elements['sitesearch'].value = location.hostname;
            }

            /* Children initialization */
            hide = document.getElementById('hide');
            show = document.getElementById('show');
            children = document.all != null ?
                    document.all['children'] :
                    document.getElementById('children');
            if (children != null) {
                children.style.display = 'none';
                show.style.display = 'inline';
                hide.style.display = 'none';
            }
        }

        function showChildren() {
            children.style.display = 'block';
            show.style.display = 'none';
            hide.style.display = 'inline';
        }

        function hideChildren() {
            children.style.display = 'none';
            show.style.display = 'inline';
            hide.style.display = 'none';
        }
    </script>
    <title>Struts 2 Themes</title>
</head>
<body onload="init()">
<table border="0" cellpadding="2" cellspacing="0" width="100%">
    <tr class="topBar">
        <td align="left" valign="middle" class="topBarDiv" align="left" nowrap>
            &nbsp;<a href="home.html">Home</a>&nbsp;&gt;&nbsp;<a href="tutorials.html">Tutorials</a>&nbsp;&gt;&nbsp;<a href="getting-started.html">Getting Started</a>&nbsp;&gt;&nbsp;<a href="struts-2-themes.html">Struts 2 Themes</a>
        </td>
        <td align="right" valign="middle" nowrap>
            <form name="search" action="https://www.google.com/search" method="get">
                <input type="hidden" name="ie" value="UTF-8" />
                <input type="hidden" name="oe" value="UTF-8" />
                <input type="hidden" name="domains" value="" />
                <input type="hidden" name="sitesearch" value="" />
                <input type="text" name="q" maxlength="255" value="" />
                <input type="submit" name="btnG" value="Google Search" />
            </form>
        </td>
    </tr>
</table>

<div id="PageContent">
    <div class="pageheader" style="padding: 6px 0px 0px 0px;">
        <!-- We'll enable this once we figure out how to access (and save) the logo resource -->
        <!--img src="/wiki/images/confluence_logo.gif" style="float: left; margin: 4px 4px 4px 10px;" border="0"-->
        <div style="margin: 0px 10px 0px 10px" class="smalltext">Apache Struts 2 Documentation</div>
        <div style="margin: 0px 10px 8px 10px"  class="pagetitle">Struts 2 Themes</div>

        <div class="greynavbar" align="right" style="padding: 2px 10px; margin: 0px;">
            <a href="https://cwiki.apache.org/confluence/pages/editpage.action?pageId=23337450">
                <img src="https://cwiki.apache.org/confluence/images/icons/notep_16.gif"
                     height="16" width="16" border="0" align="absmiddle" title="Edit Page"></a>
            <a href="https://cwiki.apache.org/confluence/pages/editpage.action?pageId=23337450">Edit Page</a>
            &nbsp;
            <a href="https://cwiki.apache.org/confluence/pages/listpages.action?key=WW">
                <img src="https://cwiki.apache.org/confluence/images/icons/browse_space.gif"
                     height="16" width="16" border="0" align="absmiddle" title="Browse Space"></a>
            <a href="https://cwiki.apache.org/confluence/pages/listpages.action?key=WW">Browse Space</a>
            &nbsp;
            <a href="https://cwiki.apache.org/confluence/pages/createpage.action?spaceKey=WW&fromPageId=23337450">
                <img src="https://cwiki.apache.org/confluence/images/icons/add_page_16.gif"
                     height="16" width="16" border="0" align="absmiddle" title="Add Page"></a>
            <a href="https://cwiki.apache.org/confluence/pages/createpage.action?spaceKey=WW&fromPageId=23337450">Add Page</a>
            &nbsp;
            <a href="https://cwiki.apache.org/confluence/pages/createblogpost.action?spaceKey=WW&fromPageId=23337450">
                <img src="https://cwiki.apache.org/confluence/images/icons/add_blogentry_16.gif"
                     height="16" width="16" border="0" align="absmiddle" title="Add News"></a>
            <a href="https://cwiki.apache.org/confluence/pages/createblogpost.action?spaceKey=WW&fromPageId=23337450">Add News</a>
        </div>
    </div>

    <div class="pagecontent">
        <div class="wiki-content">
            <div id="ConfluenceContent"><div class="confluence-information-macro confluence-information-macro-information"><span class="aui-icon aui-icon-small aui-iconfont-info confluence-information-macro-icon"></span><div class="confluence-information-macro-body"><p>The example code for this tutorial, themes, is available for checkout at <a shape="rect" class="external-link" href="https://github.com/apache/struts-examples" rel="nofollow">https://github.com/apache/struts-examples</a></p></div></div><h3 id="Struts2Themes-Introduction">Introduction</h3><p>When you use a Struts 2 tag such as s:select in your web page, the Struts 2 framework generates HTML that styles the appearance and controls the layout of the select control. The style and layout is determined by which Struts 2 theme is set for the tag. Struts 2 comes with three built-in themes: simple, xhtml, and css_xhtml. If you don&#8217;t specify a theme, then Struts 2 will use the xhtml theme by default.</p><div class="confluence-information-macro confluence-information-macro-tip"><span class="aui-icon aui-icon-small aui-iconfont-approve confluence-information-macro-icon"></span><div class="confluence-information-macro-body"><p>The <a shape="rect" class="external-link" href="http://struts.apache.org/mail.html">Struts 2 user mailing list</a> is an excellent place to get help. If you are having a problem getting the tutorial example applications to work search the Struts 2 mailing list. If you don't find an answer to your problem, post a question on the mailing list.</p></div></div><p>For example, this Struts 2 select tag:</p><div class="code panel pdl" style="border-width: 1px;"><div class="codeHeader panelHeader pdl" style="border-bottom-width: 1px;"><b>Struts 2 Select Tag</b></div><div class="codeContent panelContent pdl">
<pre class="brush: xml; gutter: false; theme: Default" style="font-size:12px;">&lt;s:select key="personBean.sport" list="sports" /&gt;
</pre>
</div></div><p>generates this HTML markup:</p><div class="code panel pdl" style="border-width: 1px;"><div class="codeHeader panelHeader pdl" style="border-bottom-width: 1px;"><b>HTML Created By Struts 2 Select Tag</b></div><div class="codeContent panelContent pdl">
<pre class="brush: xml; gutter: false; theme: Default" style="font-size:12px;">&lt;tr&gt;
&lt;td class="tdLabel"&gt;
&lt;label for="save_personBean_sport" class="label"&gt;Favorite sport:&lt;/label&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;select name="personBean.sport" id="save_personBean_sport"&gt;
    &lt;option value="football"&gt;football&lt;/option&gt;
    &lt;option value="baseball"&gt;baseball&lt;/option&gt;
    &lt;option value="basketball" selected="selected"&gt;basketball&lt;/option&gt;
&lt;/select&gt;
&lt;/td&gt;
&lt;/tr&gt;
</pre>
</div></div><p>Notice how the HTML generated uses table tags to control the layout of the label and select HTML. There is also a class, tdLabel, applied to the table column where the label tag is rendered. Since no theme was specified for the Struts 2 select tag the default xhmtl theme was used.</p><h3 id="Struts2Themes-SpecifyingTheThemeStruts2ShouldUse">Specifying The Theme Struts 2 Should Use</h3><p>The Struts 2 tags have a theme attribute you can use to specify which Struts 2 theme should be used when creating the HTML for that tag. The values for the theme attribute are simple, xhtml, css_xhtml, and ajax. To learn more about these themes visit <a shape="rect" href="themes-and-templates.html">Themes and Templates Documentation</a>. This tutorial will review the xhtml, css_xhtml, and the simple themes. The ajax theme is a special theme used for ajax operations (see <a shape="rect" href="ajax-theme.html">Ajax Theme in the documentation</a>).</p><p>You can specify the theme on a per Struts 2 tag basis or you can use one of the following methods to specify what theme Struts 2 should use:</p><ol><li>The theme attribute on the specific tag</li><li>The theme attribute on a tag's surrounding form tag</li><li>The page-scoped attribute named "theme"</li><li>The request-scoped attribute named "theme"</li><li>The session-scoped attribute named "theme"</li><li>The application-scoped attribute named "theme"</li><li>The struts.ui.theme property in struts.properties (defaults to xhtml)</li></ol><p>Consult <a shape="rect" href="selecting-themes.html">Selecting Themes</a> for how to set the theme using the above approaches.</p><p>In the example application, examine edit.jsp. The theme attribute of the form tag is set to xhtml. Run the application (see the readme.txt file) and view the source for edit.jsp after it is rendered in your browser. You should see the form HTML tags layout controlled by table tags.</p><p>Change the theme to simple for the form&#8217;s theme attribute and redeploy the application. Examine the source for edit.jsp after it is rendered in the browser. You should see that there are no table tags controlling the layout and also there are no label tags for the text fields.</p><p>Change the theme to css_xhtml for the form&#8217;s theme attribute and redeploy the application. Examine the source for edit.jsp after it is rendered in the browser. The layout of the form tags is now controlled by div tags and the label tags are back.</p><h3 id="Struts2Themes-SpecifyingTheCSSUsedByTheStruts2Tag">Specifying The CSS Used By The Struts 2 Tag</h3><p>Change the theme attribute for the form tag back to xhtml. Notice when you view the source of edit.jsp after it is rendered in the browser that there is a class named tdLabel applied to the table column that contains the label. This CSS class controls the position of the label in the table column. The tdLabel style is defined in /themes/struts/xhtml/styles.css. The link to this style sheet was included in edit.jsp&#8217;s head section when you add the s:head tag to edit.jsp.</p><p>Load this style sheet in your browser (in the example application the link is <a shape="rect" class="external-link" href="http://localhost:8080/themes/struts/xhtml/styles.css" rel="nofollow">http://localhost:8080/themes/struts/xhtml/styles.css</a> if your Servlet container is running on localhost, port 8080). You&#8217;ll see the following:</p><div class="code panel pdl" style="border-width: 1px;"><div class="codeHeader panelHeader pdl" style="border-bottom-width: 1px;"><b>styles.css</b></div><div class="codeContent panelContent pdl">
<pre class="brush: css; gutter: false; theme: Default" style="font-size:12px;">.label {font-style:italic; }
.errorLabel {font-style:italic; color:red; }
.errorMessage {font-weight:bold; color:red; }
.checkboxLabel {}
.checkboxErrorLabel {color:red; }
.required {color:red;}
.tdLabel {text-align:right; vertical-align:top; }
</pre>
</div></div><p>So the .label selector renders the label tag&#8217;s text in italic. The .tdLabel tag specifies that the text should align to the right and top of the table column.</p><p>You can override the above selectors by including the same selectors in your page&#8217;s head section. For example add the following to the head section of edit.jsp.</p><div class="code panel pdl" style="border-width: 1px;"><div class="codeHeader panelHeader pdl" style="border-bottom-width: 1px;"><b>Override Label Style</b></div><div class="codeContent panelContent pdl">
<pre class="brush: xml; gutter: false; theme: Default" style="font-size:12px;">&lt;style type="text/css"&gt;
  .label {color:blue; font-style:normal; font-weight:bold}
&lt;/style&gt;
</pre>
</div></div><p>Now the label tag will render the text in blue, bold, normal (not italics) style.</p><h3 id="Struts2Themes-CreatingAndApplyingYourOwnThemesForStruts2Tags">Creating And Applying Your Own Themes For Struts 2 Tags</h3><p>The theme templates (simple, xhtml, css_xhtml) can be found in the Struts 2 core jar file. If you expand (extract the files) the Struts 2 core jar file you'll find folders named template.css_xhtml, template.simple, and template.xhtml. Those folders contain the templates for the three default Struts 2 themes. In each folder is a file for each Struts 2 tag. For example if you expand the template.xhtml folder you&#8217;ll see the select.ftl file.</p><p>The Struts 2 framework uses the FreeMarker template engine to generate the HTML for the Struts 2 tags. That's why the file extension is ".ftl". You can learn more about FreeMarker by visiting <a shape="rect" class="external-link" href="http://freemarker.sourceforge.net/" rel="nofollow">http://freemarker.sourceforge.net/</a>.</p><p>Also in the template.xhmtl folder is the styles.css file. This is the styles.css file that your application will use if it uses the xhtml theme.</p><p>Let&#8217;s say we wanted to create our own theme that will change how the Struts 2 checkboxlist tag displays the checkboxes and their labels.</p><p>In the example application I've extended the default XHMTL theme (see file theme.properties under src/main/resources/template/KUTheme). &#160;The checkboxlist.ftl theme that is part of the XHTML theme only includes a space between each label and the next checkbox (see checkboxlist.ftl in the template/simple folder in Struts 2 core). That is why all the checkboxes are displayed across the width of the browser window. For my custom checkboxlist theme I want to have a break tag after each label tag so that each checkbox and its label will be on their own line.</p><p>In the example application there is a folder named src/main/resources/template/KUTheme. In that folder is a checkboxlist.ftl, the contents of which I originally copied from the checkboxlist.ftl that is in the templates.xhtml folder from the struts 2 core jar.</p><p>I then modified the checkboxlist.ftl in the KUTheme folder to be:</p><div class="code panel pdl" style="border-width: 1px;"><div class="codeHeader panelHeader pdl" style="border-bottom-width: 1px;"><b>Modified checkboxlist.ftl</b></div><div class="codeContent panelContent pdl">
<pre class="brush: xml; gutter: false; theme: Default" style="font-size:12px;">&lt;#include "/${parameters.templateDir}/${parameters.expandTheme}/controlheader.ftl" /&gt;

&lt;#include "/${parameters.templateDir}/KUTheme_simple/checkboxlist.ftl" /&gt;

&lt;#include "/${parameters.templateDir}/${parameters.expandTheme}/controlfooter.ftl" /&gt;&lt;#nt/&gt;
</pre>
</div></div><p>Be sure to note the change to the second line&#8212;using KUTheme_simple in the path.</p><p>Then in the example application I created a KUTheme_simple folder under <code>src/main/resources/template</code> (optionally you can place it under <code>webapp</code>, e.g. <code>src/main/webapp/template</code>). In that folder I created checkboxlist.ftl and copied the contents from template.simple checkboxlist.ftl (again found in the Struts 2 core jar). After copying the contents to checkboxlist.ftl that is in KUTheme_simple folder, I modified checkboxlist.ftl so that the label tag has a style of red bold text and I added a break tag after each label so that each check box and label will be on its own line.</p><p>Since the XHTML theme is the default theme and I have a theme.properties file defined with parent = xhtml, the KUTheme will inherit all the themes from xhmtl exempt for the theme for the checkboxlist tag since my KUTheme includes a definition for that tag's layout. &#160;In the struts.xml file (src/main/resources) you will see that the I've specified the default theme to be KUTheme.</p><p>In the deployed web application, Struts 2 will first look for a tag's template on the application's class path&#160;and if it doesn't find the template there it will use the default template that is part of the Struts 2 core jar. Since we've added a template folder to the application's web root, now when Struts 2 creates the HTML to display the checkboxlist tag it will use the template that is in the KUTheme folder (which tells it to use the checkboxlist.ftl file that is in the KUTheme_simple folder instead of the one in the template.simple folder that is part of the Struts 2 core Jar).</p><p>After redeploying the application the check boxes for the Car Models Owned should appear like:</p><p><span class="confluence-embedded-file-wrapper"><img class="confluence-embedded-image confluence-content-image-border" src="struts-2-themes.data/Screen%20shot%202010-09-11%20at%2012.37.12%20PM.png"></span></p><h3 id="Struts2Themes-Summary">Summary</h3><p>You can easily override the default theme used by Struts 2 to control the appearance and layout of a Struts 2 tag. Each Struts 2 tag has an associated template file (e.g. select.ftl) that is in a folder named after the theme (e.g. xhtml). By default the Struts framework will look in the Struts 2 core Jar file for the theme folder and templates. However, if you include your own theme folder (e.g. KUTheme) under webapp/template (or WebContent/template in the Ant version) and specify that folder name (e.g. KUTheme) as the value for the theme attribute, then the Struts 2 framework will look in that theme folder for the tag's template.</p><p>To learn more about how to use the Struts 2 themes and how you can override them, visit <a shape="rect" href="themes-and-templates.html">Themes and Templates Documentation</a>.</p><h3 id="Struts2Themes-UpNext">Up Next</h3><p>In our next tutorial we'll cover how to use the Spring and Struts 2 frameworks together.</p><div class="table-wrap"><table class="confluenceTable"><tbody><tr><th colspan="1" rowspan="1" class="confluenceTh"><p>Next</p></th><td colspan="1" rowspan="1" class="confluenceTd"><p>Onward to <a shape="rect" href="spring-and-struts-2.html">Spring and Struts 2</a></p></td></tr></tbody></table></div></div>
        </div>

        
    </div>
</div>
<div class="footer">
    Generated by CXF SiteExporter
</div>
</body>
</html>
